﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MusicLine.BusinessEntities;
using System.ComponentModel;
using MusicLine.DAL;

namespace MusicLine
{
    [DataObject(true)]
    public class MusicLineController
    {
        #region Fields and Properties
        public Comparison<CutBase> CutComparer { get; set; }
        public Comparison<Album> AlbumComparer { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the MusicLineController class.
        /// </summary>
        public MusicLineController()
            : this(null, null)
        {
        }
        /// <summary>
        /// Initializes a new instance of the MusicLineController class.
        /// </summary>
        public MusicLineController(Comparison<CutBase> cutComparer)
            : this(cutComparer, null)
        {
        }
        /// <summary>
        /// Initializes a new instance of the MusicLineController class.
        /// </summary>
        public MusicLineController(Comparison<Album> albumComparer)
            : this(null, albumComparer)
        {
        }
        /// <summary>
        /// Initializes a new instance of the MusicLineController class.
        /// </summary>
        public MusicLineController(Comparison<CutBase> cutComparer, Comparison<Album> albumComparer)
        {
            CutComparer = cutComparer;
            AlbumComparer = albumComparer;
        }
        #endregion

        #region Private Methods
        private IList<TObject> Presort<TObject>(IList<TObject> stuffToSort, Comparison<TObject> Comparer)
        {
            List<TObject> sortable = (List<TObject>)stuffToSort;
            if (Comparer != null)
                sortable.Sort(Comparer);
            return sortable;
        } // end of Presort<TObject>
        #endregion

        #region Read Methods
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IList<CutBase> GetAllCuts()
        {
            Cut_DS<CutBase> objDAL = new Cut_DS<CutBase>();
            return objDAL.GetAllCuts();
        } // end of GetAllCuts()

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IList<Album> GetAllCutsAndAlbums()
        {
            Album_DS<Album> albumRepository = new Album_DS<Album>();
            Cut_DS<CutBase> songRepository = new Cut_DS<CutBase>();
            IList<Album> albums = Presort<Album>(albumRepository.GetAllAlbums(), AlbumComparer);
            foreach (Album record in albums)
            {
                record.Cuts = Presort<CutBase>(songRepository.LookupByAlbum(record.AlbumId), CutComparer);
            }
            return albums;
        } // end of GetAllCutsAndAlbums()


        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IList<CutBase> LookupCuts(String Title)
        {
            return (new Cut_DS<CutBase>()).LookupByTitle(Title);
        } // end of LookupCuts()

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IList<CutBase> LookupCutsByArtist(string ArtistName)
        {
            return (new Cut_DS<CutBase>()).LookupByArtist(ArtistName);
        } // end of LookupCutsByArtist()
        #endregion
    } // end of class
} // end of namespace
